from django.shortcuts import render
from django.shortcuts import HttpResponse
from web_models import models
# Create your views here.
def handle_server(request):
    if request.method == "POST":
        server_id = request.POST.get("server_id","")
        handle = request.POST.get("handle","")
        if server_id != "" and handle != "":

            result = server(server_id,handle)
            return HttpResponse(result)

def server(server_id,handle):

    import commands
    handle = int(handle)
    server_obj = models.WebServer.objects.filter(id=server_id)
    ip = server_obj[0].host.ip
    port = server_obj[0].host.port

    print ip,port
    if handle == 100:
        cmd = 'tomserver tomcat start'
    else:
        print handle
    print cmd
    print("ssh {0} -p {1} {2}".format(ip,port,cmd))
    a = commands.getstatusoutput("ssh {0} -p {1} {2}".format(ip,port,cmd))
    print a